package com.example.github.enum_use;

enum Color {
    RED, BLUE, YELLOW
}

public class IterateEnum {
    public static void main(String[] args) {
        // 枚举遍历
        for (Color c : Color.values()) {
            System.out.print(c + "   "); // 遍历元素值
            System.out.println(c + " 索引 ============> " + c.ordinal()); // 遍历索引值
        }
        // valueOf 用法
        try {

        /*
        Exception in thread "main" java.lang.IllegalArgumentException: No enum constant com.example.github.enum_use.Color.WHITE
            at java.lang.Enum.valueOf(Enum.java:238)
            at com.example.github.enum_use.Color.valueOf(IterateEnum.java:3)
            at com.example.github.enum_use.IterateEnum.main(IterateEnum.java:14)

        Process finished with exit code 1
         */
            System.out.println(Color.valueOf("WHITE")); // 如果不存在枚举值会报IllegalArgumentException 异常
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } finally {
            System.out.println(Color.valueOf("RED"));
        }
    }
}
